check for `CARGO_INCREMENTAL` and pass `-Zincremental` if present
authorNiko Matsakis <niko@alum.mit.edu>
Wed, 11 Jan 2017 16:20:36 +0000 (11:20 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Wed, 11 Jan 2017 16:23:07 +0000 (11:23 -0500)
src/cargo/ops/cargo_rustc/context.rs
src/cargo/ops/cargo_rustc/layout.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/build.rs

index a5a16b4791d672411cf6cfca1945ef58f6bf9583..c3f4212e2d32705eb736f8f17ea26005ca2b1853 100644 (file)
@@ -48,6 +48,7 @@ pub struct Context<'a, 'cfg: 'a> {
     target_info: TargetInfo,
     host_info: TargetInfo,
     profiles: &'a Profiles,
+    incremental_enabled: bool,
 }
 
 #[derive(Clone, Default)]
@@ -74,6 +75,11 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             None => None,
         };
 
+        // Enable incremental builds if the user opts in. For now,
+        // this is an environment variable until things stabilize a
+        // bit more.
+        let incremental_enabled = env::var("CARGO_INCREMENTAL").is_ok();
+
         Ok(Context {
             ws: ws,
             host: host_layout,
@@ -93,6 +99,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
             build_explicit_deps: HashMap::new(),
             links: Links::new(),
             used_in_plugin: HashSet::new(),
+            incremental_enabled: incremental_enabled,
         })
     }
 
@@ -843,6 +850,14 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
         self.lib_profile()
     }
 
+    pub fn incremental_args(&self, _unit: &Unit) -> CargoResult<Vec<String>> {
+        if self.incremental_enabled {
+            Ok(vec![format!("-Zincremental={}", self.host.incremental().display())])
+        } else {
+            Ok(vec![])
+        }
+    }
+
     pub fn rustflags_args(&self, unit: &Unit) -> CargoResult<Vec<String>> {
         env_args(self.config, &self.build_config, unit.kind, "RUSTFLAGS")
     }
index 2b5cd7514d8baa8af037dfcaf0c98085ad5879be..a3570042e6356567c012c358e9a1cf8faa7a5ce7 100644 (file)
 //!         $pkg2/
 //!         $pkg3/
 //!
+//!     # Directory used to store incremental data for the compiler (when
+//!     # incremental is enabled.
+//!     incremental/
+//!
 //!     # Hidden directory that holds all of the fingerprint files for all
 //!     # packages
 //!     .fingerprint/
@@ -57,6 +61,7 @@ pub struct Layout {
     deps: PathBuf,
     native: PathBuf,
     build: PathBuf,
+    incremental: PathBuf,
     fingerprint: PathBuf,
     examples: PathBuf,
     _lock: FileLock,
@@ -88,6 +93,7 @@ impl Layout {
             deps: root.join("deps"),
             native: root.join("native"),
             build: root.join("build"),
+            incremental: root.join("incremental"),
             fingerprint: root.join(".fingerprint"),
             examples: root.join("examples"),
             root: root,
@@ -102,6 +108,7 @@ impl Layout {
 
         mkdir(&self.deps)?;
         mkdir(&self.native)?;
+        mkdir(&self.incremental)?;
         mkdir(&self.fingerprint)?;
         mkdir(&self.examples)?;
         mkdir(&self.build)?;
@@ -120,6 +127,7 @@ impl Layout {
     pub fn deps(&self) -> &Path { &self.deps }
     pub fn examples(&self) -> &Path { &self.examples }
     pub fn root(&self) -> &Path { &self.root }
+    pub fn incremental(&self) -> &Path { &self.incremental }
     pub fn fingerprint(&self) -> &Path { &self.fingerprint }
     pub fn build(&self) -> &Path { &self.build }
 }
index adbdbedc3c098e0cf8b25bbfa002d2abfb0412eb..4bcf363df19189efe74073f6eceb4139cd08bccd 100644 (file)
@@ -278,6 +278,7 @@ fn rustc(cx: &mut Context, unit: &Unit, exec: Arc<Executor>) -> CargoResult<Work
     let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
     let cwd = cx.config.cwd().to_path_buf();
 
+    rustc.args(&cx.incremental_args(unit)?);
     rustc.args(&cx.rustflags_args(unit)?);
     let json_messages = cx.build_config.json_messages;
     let package_id = unit.pkg.package_id().clone();
index d4ea324a11c06c8d104832b224f1ef16d6431808..b8c5b693cca9dec08ffb15819d6833ba3b17b1c5 100644 (file)
@@ -29,6 +29,24 @@ fn cargo_compile_simple() {
                 execs().with_status(0).with_stdout("i am foo\n"));
 }
 
+/// Check that the `CARGO_INCREMENTAL` environment variable results in
+/// `rustc` getting `-Zincremental` passed to it.
+#[test]
+fn cargo_compile_incremental() {
+    if !is_nightly() {
+        return
+    }
+
+    let p = project("foo")
+        .file("Cargo.toml", &basic_bin_manifest("foo"))
+        .file("src/foo.rs", &main_file(r#""i am foo""#, &[]));
+
+    assert_that(
+        p.cargo_process("build").arg("-v").env("CARGO_INCREMENTAL", "1"),
+        execs().with_stderr_contains(
+            "     Running `rustc [..] -Zincremental=[..]/target/debug/incremental`\n"));
+}
+
 #[test]
 fn cargo_compile_manifest_path() {
     let p = project("foo")